package com.chx.service.Impl;

import com.chx.dto.StockLogPageQueryDto;
import com.chx.entity.StockLogs;
import com.chx.mapper.ProductMapper;
import com.chx.mapper.StockLogMapper;
import com.chx.mapper.UserMapper;
import com.chx.result.PageResult;
import com.chx.service.StockLogService;
import com.chx.wrapper.StockLogWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author cp-月入过万
 * @Date 2025/5/31
 */
@Service
public class StockLogServiceImpl implements StockLogService {
    @Autowired
    private StockLogMapper stockLogMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult pageQuery(StockLogPageQueryDto stockLogPageQueryDto) {
        PageHelper.startPage(stockLogPageQueryDto.getPage(), stockLogPageQueryDto.getPageSize());
        Page<StockLogs> page = stockLogMapper.pageQuery(stockLogPageQueryDto);
        long total = page.getTotal();
        List<StockLogs> records = page.getResult();
        List<StockLogWrapper> stockLogWrappers = records.stream().map(record ->{
            String userName = userMapper.getById(record.getUserId()).getUsername();
            String productName = productMapper.getById(record.getProductId()).getProductName();
            return new StockLogWrapper(record, userName, productName);
        }).collect(Collectors.toList());
        return new PageResult(total, stockLogWrappers);
    }
}
